/**
 * 
 * @desc   千分位分隔符
 * @param  {Number} num 
 * @param  {Number} decimal 
 * @return {NumStr}
 */
function toThousands(num, decimal) {
    if (!num && num !== 0) {
        return
    }
    num = num + ''
    const reg = /\d{1,3}(?=(\d{3})+$)/g
    let intNum = ''
    let decimalNum = ''
    if (num.indexOf('.') > -1) {
        intNum = num.substring(0, num.indexOf('.'))
        decimalNum = num.substring(num.indexOf('.') + 1, num.length)
        const cimal = parseFloat(num).toFixed(decimal);
        const rest = cimal.substring(cimal.indexOf('.') + 1, cimal.length);
        const dec = decimal ? rest : '';
        return (intNum + '').replace(reg, '$&,') + '.' + (decimal ? dec : decimalNum)
    } else {
        const cimal = parseInt(10).toFixed(decimal);
        const rest = cimal.substring(cimal.indexOf('.') + 1, cimal.length);
        const dec = decimal ? '.' + rest : '';
        return (num + '').replace(reg, '$&,') + dec;
    }
}

module.exports = toThousands;